package com.soft.springhrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.springhrms.mapper.EmployeesMapper;
import com.soft.springhrms.mapper.PerformancesMapper;
import com.soft.springhrms.pojo.Employees;
import com.soft.springhrms.pojo.Performances;
import com.soft.springhrms.pojo.Requests;
import com.soft.springhrms.service.PerformancesService;
import com.soft.springhrms.vo.PerformanceVO;
import com.soft.springhrms.vo.RequestVO;
import com.soft.springhrms.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author DELL
 * @description 针对表【performances】的数据库操作Service实现
 * @createDate 2024-11-16 18:48:18
 */
@Service
public class PerformancesServiceImpl extends ServiceImpl<PerformancesMapper, Performances> implements PerformancesService {
    @Autowired
    private PerformancesMapper performancesMapper;
    @Autowired
    private EmployeesMapper employeesMapper;

    @Override
    public ResultVO queryPerformancesById(Integer id, int pageNum, int pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("employee_id", id);
        Page<Performances> page = new Page<>(pageNum, pageSize);
        // 执行分页查询
        IPage<Requests> resultPage = performancesMapper.selectPage(page, queryWrapper);
        // 判断结果是否为空
        if (resultPage.getRecords().isEmpty()) {
            return ResultVO.success(resultPage, "暂无数据");
        }
        return ResultVO.success(resultPage, "查询成功");
    }

    @Override
    public ResultVO queryAllPerformances(int pageNum, int pageSize) {
        try {
            Page<Performances> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Performances> queryWrapper = new QueryWrapper<>(); // 初始化QueryWrapper对象
            // 执行分页查询
            IPage<Performances> resultPage = performancesMapper.selectPage(page, queryWrapper);

            List<PerformanceVO> performanceVOList = new ArrayList<>();

            // 通过 employeeId 查询对应的 fullName
            for (Performances performances : resultPage.getRecords()) {
                PerformanceVO performanceVO = new PerformanceVO();
                BeanUtils.copyProperties(performances, performanceVO);

                Integer employeeId = performances.getEmployeeId();
                Employees employee = employeesMapper.selectById(employeeId); // 根据 employeeId 查询 Employee

                if (employee != null) {
                    performanceVO.setEmployeeName(employee.getFullName()); // 设置 fullName
                    performanceVO.setBonusSalary(employee.getBonusSalary());
                    performanceVO.setBaseSalary(employee.getBaseSalary());
                }

                performanceVOList.add(performanceVO);
            }
            // 包装数据并返回
            IPage<PerformanceVO> performanceVOPage = new Page<>();
            performanceVOPage.setRecords(performanceVOList);
            performanceVOPage.setTotal(resultPage.getTotal());

            return ResultVO.success(performanceVOPage, "查询成功");
        } catch (Exception e) {
            return ResultVO.error(500, "查询失败: " + e.getMessage());
        }
    }

    @Override
    public ResultVO updatePerformances(Performances performances) {
        performancesMapper.updateById(performances);
        int rows = performancesMapper.updateById(performances);
        if (rows > 0) {
            return ResultVO.success("审批成功");
        } else {
            return ResultVO.error(404, "未找到对应请求记录");
        }
    }

    @Transactional
    @Override
    public ResultVO addPerformancesAll() {
        // 查询所有员工
        List<Employees> employees = employeesMapper.selectList(null);

        if (employees.isEmpty()) {
            return ResultVO.error(400,"没有员工数据，无法添加绩效记录");
        }

        // 构建绩效记录
        List<Performances> performancesList = employees.stream().map(employee -> {
            Performances performances = new Performances();
            performances.setEmployeeId(employee.getId());
            performances.setPerformanceScore(100); // 默认评分
            performances.setPerformanceFeedback("暂未填写绩效反馈");
            performances.setPerformanceIndicator("暂未填写绩效指标");
            performances.setCreatedAt(new Date());
            return performances;
        }).collect(Collectors.toList());

        // 批量插入
        boolean isSaved = this.saveBatch(performancesList);
        if (!isSaved) {
            return ResultVO.error(400,"绩效记录批量插入失败，请重试");
        }

        return ResultVO.success(performancesList, "绩效记录批量插入成功");
    }


    @Override
    public ResultVO addPerformancesById(Integer employeeId) {
        Performances performances = new Performances();
        performances.setEmployeeId(employeeId);
        performances.setPerformanceScore(100); // 默认评分
        performances.setPerformanceFeedback("暂未填写绩效反馈");
        performances.setPerformanceIndicator("暂未填写绩效指标");
        performances.setCreatedAt(new Date());
        boolean isSaved = this.save(performances);
        if (!isSaved) {
            return ResultVO.error(400,"绩效记录插入失败，请重试");
        }

        return ResultVO.success(200,"绩效记录插入成功");
    }

}




